package leetcode.L108;

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        int size = nums.length;
        if (size == 0) {
            return null;
        }
        int left = 0;
        int right = size - 1;
        return rebuild(nums, left, right);
    }

    public TreeNode rebuild(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int index = (left + right) / 2 + 1;
        TreeNode root = new TreeNode(nums[index]);
        root.left = rebuild(nums, left, index - 1);
        root.right = rebuild(nums, index + 1, right);
        return root;
    }
}